#ifndef DBENV_H_
#define DBENV_H_

#include <string>
#include <list>
#include "status.hxx"

class DataStoreImpl;
class LogManager;
class CompactOption;
class Compact;

//the implementation of DbEnv class
class DbEnvImpl {

private:
	std::string m_home;
	std::string m_logdir;
    std::string m_logmsgFile;
	LogManager* m_logmgr;

	uint32_t m_lgBufSize;

    //the list is accessed during sync and startup, so no conflict access
    std::list<DataStoreImpl*> m_datastores;

    //for sync, only 1 sync at a time
    pthread_mutex_t m_mutex;

    //compact
    Compact* m_compact;
    uint32_t m_flag;

public:
	DbEnvImpl(const std::string& home, 
              const std::string& logpath, 
              const std::string& logmsgFile, 
              uint32_t lgBufSize);

    ~DbEnvImpl();

	DbStatus open(uint32_t flag);
	DbStatus close();
    DbStatus start();

    DbStatus sync();
    DbStatus flushlog();

    //the two functions below invoked by nodatalogmanager only
    DbStatus flushSegments();
    DbStatus flushIndexArray();

	std::string getHomepath();
	std::string getLogpath();
    LogManager* getLogManager(){return m_logmgr;}
    void setLogBufferSize(uint32_t size);
	uint32_t getLogBufferSize();
    DbStatus setLogFile(const std::string& logFile);


    DbStatus compact(CompactOption& option);
    void startCompact();
    void stopCompact(bool quit);

protected:
    void addStore(DataStoreImpl* store);
    DataStoreImpl* findStore(const std::string& storeName);

    friend class DataStoreImpl;
    friend class LogManager;
    friend class NodataLogManager;
};

#endif
